<?php defined('BASEPATH') || exit('No direct script access allowed');
class Zhuoxing extends Front_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('zhuoxing_model');
		
	}
	public function index(){
		$get = $this->input->get();
		if (!empty($get)) {
			$data["name"] =  $get["name"];
			$data["mobile"] = $get["mobile"];
			$zhuoxingModel = $this->zhuoxing_model->find_by('mobile', $data["mobile"]);
			if(!empty($zhuoxingModel)){
				echo json_encode(['code'=>401, 'msg'=>"手机号已存在！"]);
				exit;
			}
			$data["company"] = $get["company"];
			$data["add"] = $get["add"];
			$data["time"] = $get["time"];
			$data["remark"] = $get["remark"];
			$data['add_time'] = date("Y-m-d h:i:s");
			$zhuoxing = $this->zhuoxing_model->insert($data);
			if ($zhuoxing) {
				$data['id'] = $zhuoxing;
				$this->sendEmail($data);
				echo json_encode(['code'=>200, 'msg'=>"预约成功"]);
				exit;
			}
		}
	}

	private function sendEmail($info) {
		// wusheng@usuretech.com jinx@usuretech.com
		$this->load->library('emailer/emailer');
		$rn = "\r\n";
        $data = array (
                    'subject' => '阜兴·世纪公馆 - 预约信息',
                    'message' => "数据ID：{$info['id']} {$rn} 姓名：{$info['name']} {$rn} 电话：{$info['mobile']} {$rn} 公司：{$info['company']} {$rn} 接车地址：{$info['add']} {$rn} 接车时间：{$info['time']} {$rn} 备注：{$info['remark']}",
                    'to'      => 'zhengxy@usuretech.com',
                );
        $result = $this->emailer->send($data, true);
        $data['to'] = 'jinx@usuretech.com';
        $this->emailer->send($data, true);
        return $result;
	}
}